//
//  ByViewStoreAcountTime.m
//  mijifen_ios
//
//  Created by mc on 2017/10/31.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewStoreAcountTime.h"

// cell
#import "ByCellStoreConsume.h"
// view
#import "ZKBombView.h"

@interface ByViewStoreAcountTime()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *placeArray;
@property (nonatomic, strong) NSArray *buttonArray;
@property (nonatomic, strong) NSArray *titleArray;
@end

@implementation ByViewStoreAcountTime

- (void)setupUI{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = BY_TABLE_COLOR;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self addSubview:self.tableView];
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    self.tableView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
}

#pragma mark - getter
- (NSArray *)placeArray{
    if (!_placeArray) {
        _placeArray = @[@"请输入会员手机号",@"",@"请输入积分数量"];
    }
    return _placeArray;
}

- (NSArray *)buttonArray{
    if (!_buttonArray) {
        _buttonArray = @[@"",@"",@"抵扣"];
    }
    return _buttonArray;
}

- (NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = @[@"开始时间：",@"结束时间：",@"消费金额："];
    }
    return _titleArray;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.placeArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1) {
        return 3;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section != 1) {
        ByCellStoreConsume *cell = [ByCellStoreConsume cellWithTableView:tableView andPlaceStr:self.placeArray[indexPath.section] andDeduct:self.buttonArray[indexPath.section]];
        return cell;
    }else{
        static NSString *indentier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        for (UIView *subview in cell.subviews) {
            [subview removeFromSuperview];
        }
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH - 20, 50)];
        [cell addSubview:bgView];
        CGFloat width = [self.titleArray[indexPath.row] getSizeOfString:16].width + 10;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, width, 30)];
        [bgView addSubview:titleLabel];
        titleLabel.font = FONT(16);
        titleLabel.text = self.titleArray[indexPath.row];
        
        if (indexPath.row != 2) {
            UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 + width, 10, SCREEN_WIDTH - 10 - width - 10 , 30)];
            [bgView addSubview:timeLabel];
            timeLabel.text = @"00:00";
            timeLabel.font = FONT(16);
            timeLabel.textAlignment = NSTextAlignmentCenter;
            timeLabel.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(timeSelect:)];
            [timeLabel addGestureRecognizer:tap];
        }else{
            UITextField *priceTf = [[UITextField alloc] initWithFrame:CGRectMake(10 + width, 10, SCREEN_WIDTH - 10 - width - 10 , 30)];
            [bgView addSubview:priceTf];
            priceTf.textAlignment = NSTextAlignmentCenter;
            priceTf.placeholder = @"请输入消费金额";
            priceTf.font = FONT(16);
        }
//        cell.textLabel.text = self.titleArray[indexPath.row];
//        cell.detailTextLabel.text = @"1次";
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section != 1) {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
        UILabel *footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, SCREEN_WIDTH - 20, 20)];
        [footerView addSubview:footerLabel];
        footerView.backgroundColor = [UIColor clearColor];
        footerLabel.font = FONT(16);
        // 测试数据
        if (section == 0) {
            footerLabel.text = @"余额：200";
        }else{
            footerLabel.text = @"剩余 5000蜜积分，可抵扣50元";
        }
        return footerView;
    }
    return [UIView new];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 35;
    }
    return 0.0001;
}


#pragma mark - 时间选择
- (void)timeSelect:(UITapGestureRecognizer *) tap{
    ZKBombView *timeView = [[ZKBombView alloc] init];
    timeView.contentMode = ZKBombContentModeDatePicker;
    timeView.popMode = ZKBombPopModeBottom;
    timeView.positionMode = ZKBombPositionModeBottom;
    timeView.pickerDidSelected = ^(NSString *text) {
        NSLog(@"%@",text);
    };
    timeView.datePickerDidSelected = ^(NSDateComponents *dateComonents) {
        NSLog(@"%@",dateComonents);
    };
    [timeView show];
}

@end
